require 'net/http'
require 'uri'
require 'top_notify/sign'

module TopNotify
  class Connection

    include Sign

    def initialize
      if TopNotify.app_key.to_s.empty? || TopNotify.app_secret.to_s.empty?
        raise ConfigurationNotFoundError.new('You need to set app_key and app_secret first before use top_notfiy.')
      end
      @params = {:app_key => TopNotify.app_key, :timestamp => Time.now.strftime('%F %T'), :sign_method => 'md5'}
      @params.merge!(:sign => sign(@params))
      @uri = URI.parse(TopNotify.service_url)
      @request = Net::HTTP::Post.new(uri.request_uri)
      @request.set_form_data(@params)
    end

    def uri
      @uri
    end

    def request
      @request
    end

  end
end
